// pages/selectCinemaAndDate/selectCinemaAndDate.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    movieName: '',
    dateList: [],
    currentSelect: 0,
    cinemaList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    var list = that.getDateList()
    that.setData({
      movieName: options.movieName,
      dateList : list
    })
    wx.request({
      url: 'http://localhost:8081/api/cinema/wxGetCinemaList',
      data: {
        movieName : that.data.movieName,
        selectDay : '2023'+'-'+that.data.dateList[0].currentDate
      },
      method: "GET",
      "content-type":"application/json",
      success: function(res){
        that.setData({
          cinemaList : res.data.result
        })
      }
    })
  },

  // 顶部时间列
  getDateList() {
    var that = this
    var list = []
    var now = new Date()
    var month = now.getMonth() + 1;
    var day = now.getDate();
    var keepDay = now.getDate();
    if (month < 10) {
      month = '0' + month;
    };

    var today
    for(var i = 0;i < 5; i++){
      if ((keepDay + i) < 10) {
        day = '0' + (keepDay + i);
      }else {
        day = keepDay + i
      }
      today = month + '-' + day
      list.push({"currentNum":i,"currentDate":today})
    }
    return list
  },

  // 时间切换监听
  changeSelect(e) {
    var that = this
    that.setData({
      currentSelect : e.currentTarget.dataset.currentnum
    })
    wx.request({
      url: 'http://localhost:8081/api/cinema/wxGetCinemaList',
      data: {
        movieName : that.data.movieName,
        selectDay : '2023'+'-'+that.data.dateList[e.currentTarget.dataset.currentnum].currentDate
      },
      method: "GET",
      "content-type":"application/json",
      success: function(res){
        that.setData({
          cinemaList : res.data.result
        })
      }
    })
  },

  // 跳转场次页
  toArrange(e) {
    var that = this
    wx.navigateTo({
      url: '../arrange/arrange?cinemaId='
       + e.currentTarget.dataset.cinema.id +
      '&cinemaName=' + e.currentTarget.dataset.cinema.cinemaName +
      '&address=' + e.currentTarget.dataset.cinema.address +
      '&currentSelect=' + that.data.dateList[that.data.currentSelect].currentNum
    })
    wx.setStorageSync('selectCinemaInfo', e.currentTarget.dataset.cinema)
    // wx.navigateTo({
    //   url: '../test/test',
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})